//
//  XianzhaoshangViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2019/1/8.
//  Copyright © 2019 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class XianzhaoshangViewController: UIViewController {
    var parentID:String!
    
    var tableView: UITableView!
    
    var XianzhaoshangArray = [XianzhaoshangModel]()
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "办理人"
        
        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 49), style: .plain )
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorInset = UIEdgeInsets.zero
        //        tableView.separatorStyle = .none
        tableView.backgroundColor = bgGrayColor
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        //tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)//去除表格上放多余的空隙
        //tableView.tableHeaderView = UIView.init()
        tableView.tableFooterView = UIView.init()
        self.view.addSubview(tableView)
        
        
        
        
        tableView.register(NSClassFromString("moreBanlirenTableViewCell"), forCellReuseIdentifier: "moreBanlirenTableViewCell")
        
        self.loadData()
    }
    func loadData(){
        
        
        let manager = UserDefaults()
        
        
        let token: String? = manager.string(forKey:"tokens")  //根据关键字取值
        
        print("1111===\(token!)")
        
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/organization/list"
        
        let json = "{\"target\":\"\("assignee")\",\"token\":\"\(token!)\",\"parentId\":\"\(self.parentID!)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("2222222222json====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array


                for dataDic in  array! {

                    let model1 = XianzhaoshangModel()
                    model1.id = dataDic["id"].string

         
                    model1.isParent = dataDic["isParent"].string

                    model1.pId = dataDic["pId"].string

                    model1.name = dataDic["name"].string
                  

                    model1.type = dataDic["type"].string

                    self.XianzhaoshangArray.append(model1)


                
                }

                self.tableView.reloadData()

            }else{

                print("数据请求失败")
            }
            
            
            
            
        }
        
    }



}

extension XianzhaoshangViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.XianzhaoshangArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let morebanliren:XianzhaoshangModel = self.XianzhaoshangArray[indexPath.row]
        

        
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            if cell == nil {
                
                cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
            }
            
            cell?.textLabel?.text = morebanliren.name
            cell?.textLabel?.font = UIFont.systemFont(ofSize:12)
            cell?.selectionStyle = UITableViewCellSelectionStyle.none
            return cell!
            

    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let banliren:XianzhaoshangModel = self.XianzhaoshangArray[indexPath.row]
    
            
            
            //实例化一个将要跳转的viewController
            let moreBanlirenVc = moreBanlirenViewController()
            moreBanlirenVc.parentID = banliren.id
            self.navigationController?.pushViewController(moreBanlirenVc , animated: true)
 
        
        
        
    }
  
        
        
        
}
